﻿function digest_blogbody(id, url, location_href)
{
  //自动文章摘要的参数设定
  var word_min = 100; //字数最少要到这种程度
  var word_max = 150; //字数最多要到这种程度
  
  count = location_href.split("/");
  if (count.length > 5 && count[4] != "label")
    return;
 
  blogitembody = document.getElementById(id).innerHTML;
  
  
  var digest_point = -1;
  var digest_mode = "auto";

  //判断是否要隐藏全文
  if (digest_mode == "auto")
  {
 
 digest_point = blogitembody.indexOf("<!--Hidden All-->", 0);
 if (digest_point != -1)
 {
   digest_point = 0;
   digest_mode = "hidden_all";
 }
  }

  if (digest_mode == "auto")
  {
 digest_point = blogitembody.indexOf("<!--Show All-->", 0);
 if (digest_point != -1)
 {
   digest_point = blogitembody.length;
   digest_mode = "show_all";
 }
  }

  if (digest_mode == "auto")
  {
 digest_point = blogitembody.indexOf("<!--Digest-->", 0);
 if (digest_point != -1)
 {
   digest_point = digest_point + 13;
   digest_mode = "digest";
 }
  }

  //手动设定输出
  if (digest_mode != "auto")
  {
   //alert(blogitembody);
   //if (digest_mode == "hidden_all")
     //digest_point = 0;
   
   if (blogitembody.toLowerCase().substr(blogitembody.length-56, 56) == '<div style="clear:both; padding-bottom: 0.25em;"></div>')
    var body_len = blogitembody.length - 56;
   else
    var body_len = blogitembody.length;
 
   //alert(blogitembody.toLowerCase().substr(blogitembody.length-56, 56) + " -- " + body_len + " -- " + digest_point);
   //alert(body_len);
 
   if (digest_point < body_len && digest_point != -1)
   {
  if (digest_mode != "hidden_all" || digest_mode != "digest")
    continue_reading1 = "……";
  else
    continue_reading1 = "";
  
  //continue_reading2 = '<a href="'+url+'" title="post link" class="link_fullpost">(继续阅读)</a>';   
  if (digest_mode == "hidden_all")
    continue_reading2 = '<span class="link_fullpost">| <a href="javascript:digest_show(\''+id+'\')" title="show only digest">Read more</a> | <a href="'+url+'" title="post link">Display only the page</a> |</span>';
  else if  (digest_mode == "digest")
    continue_reading2 = '<span class="link_fullpost">| <a href="javascript:digest_show(\''+id+'\')" title="show only digest">Read more</a> | <a href="'+url+'" title="post link">Display only the page</a> |</span>';
 
 //隱藏全文
 hidden_reading = '<span class="link_fullpost">| <a href="javascript:digest_show(\''+id+'\')" title="show only digest">Show summary only</a> | <a href="'+url+'" title="post link">Separate display the page</a> |</span>';
    blogitembody_digest_ohter = blogitembody.substr(0, digest_point) + '<a id="'+id+'-digest_point" name="'+id+'-digest_point"></a>'+blogitembody.substr(digest_point, blogitembody.length-digest_point);
 blogitembody_digest_ohter = '<div class="post_hidden" id="'+id+'-hidden" style="display:none">'+blogitembody_digest_ohter + hidden_reading + '</div>';

   }
   else
   {
  continue_reading1 = "";
  continue_reading2 = "";
  blogitembody_digest_ohter = "";
   }
   
   //处理end tag
   var end_tag = "";
   if (digest_mode == "digest")
   {
  var text = blogitembody.substr(0, digest_point);
  each_tag = text.split("<");
  
  //end_tags = new Array;
  //var tag_i = 0;
  
  for (var i = 1; i < each_tag.length; i++)
  {
  if (each_tag[i].substr(0, 1) == "/"
  || each_tag[i].substr(0, 1) == "!")
    continue;
  
  temp = each_tag[i].split(" ", 2);
  temp2 = each_tag[i].split(">", 2);
  if (temp[0].length < temp2[0].length)
    tag = temp[0];
  else
    tag = temp2[0];
  end_tag = "</" + tag + ">" + end_tag;
  //end_tags[tag_i] = temp[0];
  //tag_i++;
  }
   }
 
   blogitembody_digest = blogitembody.substr(0, digest_point) + continue_reading1 + end_tag;
   //alert(blogitembody_digest);
   //document.getElementById(id).innerHTML = blogitembody_digest+continue_reading2;
   document.getElementById(id).innerHTML = '<div name="'+id+'-show" id="'+id+'-show" class="post_show"><a name="'+id+'-show_point" id="'+id+'-show_point"></a>'+blogitembody_digest+continue_reading2+'</div>' + blogitembody_digest_ohter;
   return;
  }
  
  //--------------------------------------------------------
  //以上是手动切断设定，以下是自动切断设定
  
  //1. 初始化设定
  
  //blogitembody //文本
  
  var word_num = 0; //字数


  var words = ""; //待显示文字及标签
  
  var tags = new Array; //储存每个tag，以方便还原
  tag_i = 0;
  var dp = 0; //现在判断位置
  
  var stop_tag = new Array;
  stop_tag[0] = "img";
  stop_tag[1] = "br";
  
  var no_save = "false";
  
  var stop_chr = new Array;
  stop_chr[0] = " ";
  stop_chr[1] = "\n";
  stop_chr[2] = "\t";
  stop_chr[3] = ">";
  stop_chr[4] = "<";
  
  var chr_no_save = "false";
  
  var skip_tag = new Array;
  skip_tag[0] = "script";
  skip_tag[1] = "style";
  
  var temp_str = "";  //暂存字元，用来比较关键字
  var now_chr = ""; //现在检查字元
  
  //alert(digest_point);
  
  //一个字一个字抓取
  for (i = 0; i < blogitembody.length && digest_point == -1; i++)
  {
    now_chr = blogitembody.substr(i, 1);
  
 //检查是否要跳过
 if (now_chr == "<")
 {
   //检查是否省略标签
   is_skip_tag = "false";
   for (sk_i = 0; sk_i < skip_tag.length; sk_i++)
   {
      temp_str = blogitembody.substr(eval(i+1), skip_tag[sk_i].length);
   if (temp_str == skip_tag[sk_i])
   {
     //跳过同样的标签结束为止
      i = blogitembody.indexOf("</"+skip_tag[sk_i]+">", i);
   i = eval(i) + skip_tag[sk_i].length + 3;
   //word_num--;
   is_skip_tag = "true";
   break;
   }
   }
   if (is_skip_tag == "true")
     continue;
   
   //检查标签是否要存储
   tagname_point1 = blogitembody.indexOf(" ", i);
   tagname_point2 = blogitembody.indexOf(">", i);
   if (tagname_point2 < tagname_point1 || tagname_point1 == -1)
     tagname_point = tagname_point2;
   else
     tagname_point = tagname_point1;
   
   tagname = blogitembody.substr(eval(i+1), tagname_point - eval(i+1));
   no_save = "false";
   for (st_i = 0; st_i < stop_tag; st_i++)
   {
  if (tagname == stop_tag[st_i])
  {
    no_save = "true";
    break;
  }
   }
   
   if (no_save == "false")
   {
  tags[tag_i] = tagname;
  tag_i++;
   }
   
   //跳跃到标签结束为止
   i = blogitembody.indexOf(">", eval(i));
   continue;
 }
 
 //儲存字元
 chr_no_save = "false";
 for (sc_i = 0; sc_i < stop_chr.length; sc_i++)
 {
  if (now_chr == stop_chr[sc_i])
  {
   chr_no_save = "true";
   break;
  }
 }
 if (chr_no_save == "false" && now_chr != "<")
 {
   word_num++;
   //alert("'"+blogitembody.substr(i-1, 3)+"'");
 }
 
 //檢查是否超過限制字元
 if (word_num > word_max + tags.length)
   break;
  }
  //alert(word_num);
  
  digest_point = eval(i+1);
  
  //檢查是否有結尾標籤
  var temp_dp = blogitembody.indexOf("</", digest_point);
  if (temp_dp - digest_point < word_min && temp_dp != -1)
    digest_point = temp_dp;
  
  //標籤還原
  end_tag = "";
  //alert(tag_i);
  
  for (var i = (tag_i-1) ; i > -1; i--)
  {
    //alert("</"+ tags[i] + ">");
 if (tags[i].substr(0,1) != "/")
      end_tag = end_tag + '</'+ tags[i] + '>';
  }
  
  //alert(end_tag);
  
  //alert(blogitembody.substr(0, digest_point)); 

  if (blogitembody.toLowerCase().substr(blogitembody.length-56, 56) == '<div style="clear:both; padding-bottom: 0.25em;"></div>')
  {
   var body_len = blogitembody.length - (tag_i * 5) - 56;
  }
  else
      var body_len = blogitembody.length - (tag_i * 5);

  //alert(blogitembody.toLowerCase().substr(blogitembody.length-56, 56) + " -- " + body_len + " -- " + digest_point);

  if (digest_point < body_len && digest_point != -1)
  {
 continue_reading1 = "……";
 continue_reading2 = '<span class="link_fullpost">| <a href="javascript:digest_show(\''+id+'\')" title="show only digest">Read more</a> | <a href="'+url+'" title="post link">Display only the page</a> |</span>';
 
 //隐藏全文
 hidden_reading = '<span class="link_fullpost">| <a href="javascript:digest_show(\''+id+'\')" title="show only digest">仅显示摘要</a> | <a href="'+url+'" title="post link">Display only the page</a> |</span>';
    blogitembody_digest_ohter = blogitembody.substr(0, digest_point) + '<a id="'+id+'-digest_point" name="'+id+'-digest_point"></a>'+blogitembody.substr(digest_point, blogitembody.length-digest_point);
 blogitembody_digest_ohter = '<div class="post_hidden" id="'+id+'-hidden" style="display:none">'+blogitembody_digest_ohter + hidden_reading + '</div>';
  }
  else
  {
 continue_reading1 = "";
 continue_reading2 = "";
 
 blogitembody_digest_ohter = '';
  }
  
  //alert(end_tag);
  blogitembody_digest = blogitembody.substr(0, digest_point) + continue_reading1 + end_tag;
  document.getElementById(id).innerHTML = '<div class="post_show" name="'+id+'-show" id="'+id+'-show"><a name="'+id+'-show_point" id="'+id+'-show_point" ></a>'+blogitembody_digest+continue_reading2+"</div>\n\n" + blogitembody_digest_ohter;
}

function digest_show(id)
{
  if (document.getElementById(id+"-show").style.display == ""
 || document.getElementById(id+"-show").style.display == "block")
  {
 document.getElementById(id+"-show").style.display = "none";
 document.getElementById(id+"-hidden").style.display = "block";
 //location.href = "#"+id+"-digest_point";
  }
  else
  {
 document.getElementById(id+"-hidden").style.display = "none";
 document.getElementById(id+"-show").style.display = "block";
 location.href = "#"+id+"-post-title"; 
  }
}